/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "S_纹理.h"


#include "intern/Vulkan/buffer/Vk图像.h"
#include "底层绘图/底层绘图框架.h"



static uint32 f_转VK纹理用途(uint32 用途) {
	uint32 usage = 0;
	if (用途 & E_纹理用途::e_采样)			usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
	if (用途 & E_纹理用途::e_转移源)		usage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
	if (用途 & E_纹理用途::e_转移目标)		usage |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
	if (用途 & E_纹理用途::e_贮存)			usage |= VK_IMAGE_USAGE_STORAGE_BIT;
	if (用途 & E_纹理用途::e_颜色附件)		usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
	if (用途 & E_纹理用途::e_深度模板附件)	usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
	if (用途 & E_纹理用途::e_短时附件)		usage |= VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT;
	if (用途 & E_纹理用途::e_输入附件)		usage |= VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
	//if (用途 & E_纹理用途::e_采样) usage |= VK_IMAGE_USAGE_INVOCATION_MASK_BIT_HUAWEI;
	//if (用途 & E_纹理用途::e_采样) usage |= VK_IMAGE_USAGE_SHADING_RATE_IMAGE_BIT_NV;
	return usage;
}




static void setImageLayout(VkCommandBuffer cmd, VkImage image,
	VkImageAspectFlags aspectMask,
	VkImageLayout old_image_layout,
	VkImageLayout new_image_layout,
	int32_t  layerCount
)//设置图像布局的方法
{
	VkImageMemoryBarrier barrier = f_vk_图像屏障();
	barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
	barrier.pNext = NULL;
	barrier.srcAccessMask = 0;
	barrier.dstAccessMask = 0;
	barrier.oldLayout = old_image_layout;
	barrier.newLayout = new_image_layout;
	//barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
	//barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
	barrier.image = image;
	barrier.subresourceRange.aspectMask = aspectMask;
	barrier.subresourceRange.baseMipLevel = 0;
	barrier.subresourceRange.levelCount = 1;
	barrier.subresourceRange.baseArrayLayer = 0;
	barrier.subresourceRange.layerCount = layerCount;//数组层的数量


	VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
	VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;

	if (old_image_layout == VK_IMAGE_LAYOUT_UNDEFINED && new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
		barrier.srcAccessMask = 0;
		barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;

		src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
		dest_stages = VK_PIPELINE_STAGE_TRANSFER_BIT;
	}
	else if (old_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
		barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
		barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;

		src_stages = VK_PIPELINE_STAGE_TRANSFER_BIT;
		dest_stages = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
	}



	switch (old_image_layout) {
	case VK_IMAGE_LAYOUT_UNDEFINED: barrier.srcAccessMask = 0; break;
	case VK_IMAGE_LAYOUT_PREINITIALIZED: barrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT; break;
	case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: barrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; break;
	case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL: barrier.srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; break;
	case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL: barrier.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT; break;
	case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL: barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; break;
	case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL: barrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT; break;
	default:
		break;
	}

	switch (new_image_layout) {
	case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL: barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; break;
	case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL: barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; break;
	case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; break;
	case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL: barrier.dstAccessMask = barrier.dstAccessMask | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; break;
	case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
		if (barrier.srcAccessMask == 0) {
			barrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
		}
		barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
		break;
	default:
		break;
	}

	vkCmdPipelineBarrier(cmd, src_stages, dest_stages, 0, 0, NULL, 0, NULL, 1, &barrier);

}




S_纹理* f_tex_创建图像纹理(S_设备环境& ctx, uvec3 size, E_纹理格式 格式, E_纹理维度类型 类型, E_MS次数 采样, E_纹理排列 平铺方式) {
	S_绘图设备配置& prop = *((S_绘图设备配置*)ctx.m_逻辑设备.pNext);

	uint32 用途 = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
	S_VkImage* 图像 = S_Vk图像管理::f_创建纹理图像(prop, f_转VK图像格式(格式), size, 类型, 用途, 采样);
	图像->m_格式 = 格式;
	图像->m_类型 = 类型;
	图像->m_Size = size;
	图像->m_用途 = 用途;
	图像->m_Ctx = &ctx;

	return 图像;
}

S_纹理* f_tex_创建深度纹理(S_设备环境& ctx, uvec3 size, E_MS次数 采样, E_纹理格式 格式) {
	VkDevice 逻辑设备 = (VkDevice)(ctx.m_逻辑设备.设备);
	auto* prop = ((S_物理设备属性*)ctx.m_物理设备.pNext);

	S_VkImage* 图像 = S_Vk图像管理::f_创建深度缓存图像(逻辑设备, prop->m_内存属性, prop->m_设备属性, f_转VK图像格式(格式), size, 采样);
	图像->m_格式 = 格式;
	图像->m_类型 = E_纹理维度类型::e_2D;
	图像->m_Size = size;
	图像->m_用途 = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
	图像->m_Ctx = &ctx;

	return 图像;
}

S_纹理* f_tex_创建帧缓存纹理(S_设备环境& ctx, uvec3 size, E_纹理格式 格式, E_纹理维度类型 类型, E_MS次数 采样, E_纹理排列 平铺方式) {
	VkDevice 逻辑设备 = (VkDevice)(ctx.m_逻辑设备.设备);
	auto* prop = ((S_物理设备属性*)ctx.m_物理设备.pNext);

	//uint32 用途 = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
	uint32 用途 = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_STORAGE_BIT;
	//uint32 用途 = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
	S_VkImage* 图像 = S_Vk图像管理::f_创建帧缓存图像(逻辑设备, prop->m_内存属性, f_转VK图像格式(格式), size, 采样, 用途);
	图像->m_格式 = 格式;
	图像->m_类型 = 类型;
	图像->m_Size = size;
	图像->m_用途 = 用途;
	图像->m_Ctx = &ctx;

	return 图像;
}

S_纹理* f_tex_创建存储2D纹理(S_设备环境& ctx, uvec3 size, E_纹理格式 格式, uint32 用途, E_MS次数 采样) {
	S_绘图设备配置& 配置 = *((S_绘图设备配置*)ctx.m_逻辑设备.pNext);

	S_VkImage* 图像 = S_Vk图像管理::f_创建纹理图像(配置, f_转VK图像格式(格式), size, E_纹理维度类型::e_2D, f_转VK纹理用途(用途), 采样);
	图像->m_格式 = 格式;
	图像->m_类型 = E_纹理维度类型::e_2D;
	图像->m_Size = size;
	图像->m_用途 = f_转VK纹理用途(用途);
	图像->m_Ctx = &ctx;

	auto cmd = f_vk创建命令缓存(&配置);
	f_vk绑定命令缓存(&配置, cmd);
	setImageLayout(cmd, 图像->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL, 1);
	图像->m_着色器参数.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
	f_vk刷新命令缓存(&配置, cmd, true);

	return 图像;
}

void f_tex_销毁纹理(S_纹理* tex) {
	S_绘图设备配置* pip = (S_绘图设备配置*)(tex->m_Ctx->m_绘图设备配置);
	f_releaseImage(pip->m_设备, (S_VkImage*)tex);
	delete tex;
}



S_纹理采样器* f_tex_创建纹理采样器(S_设备环境& ctx, S_纹理采样器创建参数 参数) {
	VkDevice 逻辑设备 = (VkDevice)(ctx.m_逻辑设备.设备);
	S_Vk纹理采样器* sample = new S_Vk纹理采样器(逻辑设备, 参数);
	return sample;
}

void f_tex_安装纹理采样器(S_纹理* tex, S_纹理采样器* sample) {
	S_VkImage* 图像 = (S_VkImage*)(tex);
	图像->m_着色器参数.sampler = dynamic_cast<S_Vk纹理采样器*>(sample)->m_采样器;
}




void f_tex_填充纹理数据(S_纹理* tex, const uint8* t, uint16 offset, uvec3 dim, S_结构对象指针 命令缓存) {
	assert(dynamic_cast<S_VkImage*>(tex));
	S_VkImage* image = (S_VkImage*)(tex);
	
	uint64 num = dim.x * dim.y * dim.z;

	S_VkArray缓存* 临时缓存 = dynamic_cast<S_VkArray缓存*>(f_buf_create板载缓存(*tex->m_Ctx, offset, E_板载缓存类型::e_TBOs, num));
	f_buf_fill板载缓存(临时缓存, t, num);


	VkBufferImageCopy bufferCopyRegion = {}; //构建缓冲图像拷贝结构体实例
	bufferCopyRegion.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;	//使用方面
	bufferCopyRegion.imageSubresource.mipLevel = 0;								//mipmap 级别
	bufferCopyRegion.imageSubresource.baseArrayLayer = 0;						//基础数组层
	bufferCopyRegion.imageSubresource.layerCount = dim.z;						//数组层的数量
	bufferCopyRegion.imageExtent.width = dim.x;
	bufferCopyRegion.imageExtent.height = dim.y;
	bufferCopyRegion.imageExtent.depth = 1;



	S_绘图设备配置* 配置 = (S_绘图设备配置*)(tex->m_Ctx->m_绘图设备配置);
	VkCommandBuffer cmd_bufs[] = { (VkCommandBuffer)命令缓存 };
	if (!命令缓存) cmd_bufs[0] = 配置->m_命令缓存;
	
	

	VkFenceCreateInfo fenceCreateInfo{};
	fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
	fenceCreateInfo.flags = 0;
	VkFence fence;
	vkCreateFence(配置->m_设备, &fenceCreateInfo, nullptr, &fence);


	static VkCommandBufferInheritanceInfo InheritanceInfo{};
	InheritanceInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO;
	vkResetCommandBuffer(cmd_bufs[0], 0);
	VkCommandBufferBeginInfo cmdBufferBeginInfo{};
	cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
	cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT | VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT;
	cmdBufferBeginInfo.pInheritanceInfo = &InheritanceInfo;
	auto err = vkBeginCommandBuffer(cmd_bufs[0], &cmdBufferBeginInfo);
	

	setImageLayout(cmd_bufs[0], image->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dim.z);
	vkCmdCopyBufferToImage(cmd_bufs[0], 临时缓存->m_Data.m_Buf, image->m_Image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &bufferCopyRegion);
	setImageLayout(cmd_bufs[0], image->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, dim.z);
	

	vkEndCommandBuffer((VkCommandBuffer)cmd_bufs[0]);

	VkSubmitInfo 命令提交信息{ VK_STRUCTURE_TYPE_SUBMIT_INFO };
	命令提交信息.commandBufferCount = 1;
	命令提交信息.pCommandBuffers = (VkCommandBuffer*)&cmd_bufs[0];
	
	//auto result = vkQueueSubmit(配置->m_图形列队[配置->m_列队ID], 1, &命令提交信息, VK_NULL_HANDLE);
	//vkDeviceWaitIdle(配置->m_设备);

	auto result = vkQueueSubmit(配置->m_图形列队[配置->m_列队ID], 1, &命令提交信息, fence);
	do {
		result = vkWaitForFences(配置->m_设备, 1, &fence, VK_TRUE, 100000);
	} while (result == VK_TIMEOUT);
	vkDestroyFence(配置->m_设备, fence, nullptr);

	f_buf_release板载缓存(临时缓存);
	
}

void f_tex_读取纹理数据(S_纹理* tex, S_板载缓存* 临时缓存, S_结构对象指针 命令缓存) {
	assert(dynamic_cast<S_VkImage*>(tex));
	S_VkImage* image = (S_VkImage*)(tex);

	S_VkArray缓存* GPUBuffer = dynamic_cast<S_VkArray缓存*>(临时缓存);
	//f_buf_fill板载缓存(临时缓存, t, num, E_板载缓存类型::e_TBOs);
	VkBufferImageCopy bufferCopyRegion = {}; //构建缓冲图像拷贝结构体实例
	bufferCopyRegion.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;	//使用方面
	bufferCopyRegion.imageSubresource.mipLevel = 0;								//mipmap 级别
	bufferCopyRegion.imageSubresource.baseArrayLayer = 0;						//基础数组层
	bufferCopyRegion.imageSubresource.layerCount = tex->m_Size.z;						//数组层的数量
	bufferCopyRegion.imageExtent.width = tex->m_Size.x;
	bufferCopyRegion.imageExtent.height = tex->m_Size.y;
	bufferCopyRegion.imageExtent.depth = 1;



	S_绘图设备配置* 配置 = (S_绘图设备配置*)(tex->m_Ctx->m_绘图设备配置);
	if (!命令缓存) {
		命令缓存 = (S_结构对象指针)配置->m_命令缓存;
	}

	VkFenceCreateInfo fenceCreateInfo{};
	fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
	fenceCreateInfo.flags = 0;
	VkFence fence;
	vkCreateFence(配置->m_设备, &fenceCreateInfo, nullptr, &fence);
	f_Draw_Begin(tex->m_Ctx, 命令缓存, (S_结构对象指针)fence);


	//setImageLayout((VkCommandBuffer)命令缓存, image->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 1);
	//vkCmdCopyBufferToImage((VkCommandBuffer)命令缓存, 临时缓存->m_Data.m_Buf, image->m_Image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &bufferCopyRegion);
	vkCmdCopyImageToBuffer((VkCommandBuffer)命令缓存, image->m_Image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, GPUBuffer->m_Data.m_Buf, 1, &bufferCopyRegion);
	//setImageLayout((VkCommandBuffer)命令缓存, image->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_UNDEFINED, 1);


	f_Draw_end(tex->m_Ctx, 命令缓存, (S_结构对象指针)fence, 3);
	vkDestroyFence(配置->m_设备, fence, nullptr);

	
}


void f_tex_修改大小(S_纹理* tex, uvec2 newSize) {
	S_VkImage* 图像 = (S_VkImage*)(tex);
	S_绘图设备配置& 配置 = *((S_绘图设备配置*)tex->m_Ctx->m_逻辑设备.pNext);
	
	if (图像->m_Size.x == newSize.x && 图像->m_Size.y == newSize.y) return;
	图像->m_Size.x = newSize.x;
	图像->m_Size.y = newSize.y;

	f_VkImage_构建(图像, *图像->m_Ctx);
	
	auto cmd = f_vk创建命令缓存(&配置);
	f_vk绑定命令缓存(&配置, cmd);
	setImageLayout(cmd, 图像->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL, 1);
	图像->m_着色器参数.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
	f_vk刷新命令缓存(&配置, cmd, true);

}

void f_tex_修改大小(S_纹理* tex, uvec3 newSize) {
	S_VkImage* 图像 = (S_VkImage*)(tex);
	if (图像->m_Size.x == newSize.x && 图像->m_Size.y == newSize.y && 图像->m_Size.z == newSize.z) return;
	图像->m_Size.x = newSize.x;
	图像->m_Size.y = newSize.y;
	图像->m_Size.z = newSize.z;

	f_VkImage_构建(图像, *图像->m_Ctx);

	//S_绘图设备配置& 配置 = *((S_绘图设备配置*)tex->m_Ctx->m_逻辑设备.pNext);
	//auto cmd = f_vk创建命令缓存(&配置);
	//f_vk绑定命令缓存(&配置, cmd);
	//setImageLayout(cmd, 图像->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL, 1);
	//图像->m_着色器参数.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
	//f_vk刷新命令缓存(&配置, cmd, true);
}

void f_tex_拷贝(S_结构对象指针 cmd, S_纹理* 源, S_纹理* 目标) {
	//S_绘图设备配置& 配置 = *((S_绘图设备配置*)ctx.m_逻辑设备.pNext);
	setImageLayout((VkCommandBuffer)cmd, ((S_VkImage*)目标)->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1);
	setImageLayout((VkCommandBuffer)cmd, ((S_VkImage*)源)->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, 1);

	VkImageCopy copyRegion{};
	copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
	copyRegion.srcOffset = { 0, 0, 0 };
	copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
	copyRegion.dstOffset = { 0, 0, 0 };
	copyRegion.extent = { 源->m_Size.x, 源->m_Size.y, 1 };
	vkCmdCopyImage((VkCommandBuffer)cmd,
		((S_VkImage*)源)->m_Image,
		VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, 
		((S_VkImage*)目标)->m_Image,
		VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 
		1, 
		&copyRegion);

	setImageLayout((VkCommandBuffer)cmd, ((S_VkImage*)目标)->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, 1);
	setImageLayout((VkCommandBuffer)cmd, ((S_VkImage*)源)->m_Image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_GENERAL, 1);
}

void f_tex_拷贝(S_结构对象指针 cmd, S_纹理* 源, S_板载缓存* 目标) {
	VkImageLayout srcImageLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
	
	VkBufferImageCopy image_copy = {};

	image_copy.bufferOffset = 0;
	image_copy.bufferRowLength = 0;
	image_copy.bufferImageHeight = 0;
	image_copy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
	image_copy.imageSubresource.baseArrayLayer = 0;
	image_copy.imageSubresource.layerCount = 1;
	image_copy.imageSubresource.mipLevel = 0;
	image_copy.imageOffset = { 0,0,0 };
	image_copy.imageExtent = { 源->m_Size.x, 源->m_Size.y, 1 };

	vkCmdCopyImageToBuffer((VkCommandBuffer)cmd, ((S_VkImage*)源)->m_Image, srcImageLayout, static_cast<S_VkArray缓存*>(目标)->m_Data.m_Buf, 1, &image_copy);

}













